package com.isoftstone.merchant.controller.chargeunderline;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.common.Page;
import com.isoftstone.common.Tidings;
import com.isoftstone.merchant.entity.feeUser.MFeeUser;
import com.isoftstone.merchant.entity.mdept.MDept;
import com.isoftstone.merchant.entity.mproject.MProject;
import com.isoftstone.merchant.entity.payment.Payment;
import com.isoftstone.merchant.service.feeUser.FeeUserService;
import com.isoftstone.merchant.service.mproject.MProjectService;
import com.isoftstone.merchant.service.payment.PaymentService;
import com.isoftstone.util.Const;
import com.isoftstone.util.PageData;
import com.isoftstone.util.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.net.URLDecoder;
import java.util.*;

/**
 * Created by as on 2017/12/13.
 * 线下收费
 */
@Controller
public class ChargeUnderLineController extends BaseController{
    @Autowired
    PaymentService paymentService;
    @Autowired
    MProjectService mProjectService;
    @Autowired
    FeeUserService feeUserService;

    @RequiresPermissions("mms-menu.003.006")
    @RequestMapping("merchant/chargeUnderLine/getChargeUnderLineList")
    public ModelAndView toChargeUnderLinePage(){
        ModelAndView md=new ModelAndView();
        md.setViewName("merchant/charge_management/charge_under_line");
        return md;
    }
    @RequestMapping("merchant/chargeUnderLine/getChargeUnderLineList1")
    @ResponseBody
    public Object chargeUnderLinePage(Page page){
        PageData pd = this.getPageData();
        Tidings<List<Payment>> tidings = new Tidings<List<Payment>>();
        String corpId = this.getUser().getCorpId();
        String keywords = pd.getString("searchContent");
        String projectType=pd.getString("projectType");
        String project=pd.getString("project");
        String treeid=pd.getString("treeId");
        String userId=this.getUser().getUserid();
        if(StringUtils.isNotBlank(projectType)) {
            pd.put("projectType", Long.parseLong(projectType));
        }
        if(StringUtils.isNotBlank(project)) {
            pd.put("projectment", Long.parseLong(project));
        }
        pd.put("keywords", keywords);
        pd.put("corpId", corpId);
        pd.put("status","1");//生效的缴费
        pd.put("payStatus","04");
        List<Payment> paymentList=new ArrayList<Payment>();
        List<Payment>searchList=new ArrayList<Payment>();
        List<String >treeIdss=new ArrayList<String>();
        List<MDept>deptids=null;
        try {
            deptids=feeUserService.getalldeptByUserId(userId);//得到用户对应的deptid
            if(deptids!=null){
                List<String>list=new ArrayList<String>();
                for(MDept mDept1:deptids){
                    list.add(mDept1.getDeptId());
                }
                pd.put("deptIds",list);//看到当前用户部门的缴费用户
            }
            if(StringUtils.isNotBlank(keywords)){
              keywords = URLDecoder.decode(keywords, "UTF-8");
              pd.put("keywords", keywords);
            }
            if(StringUtils.isNotBlank(treeid)){
                String treeIds[] = treeid.split(",");
                for (String str : treeIds) {
                    treeIdss.add(str);
                }
                pd.put("treeids",treeIdss);
                page.setPd(pd);
                paymentList = paymentService.getPaymentlistPage(page);
                List<Payment> paymentList1 = paymentService.getAllPayMentlistPage(paymentList);
                tidings.setStatus(Const.SUCCESS);
                tidings.setT(paymentList1).setPage(page);
            }else {
                page.setPd(pd);
                paymentList = paymentService.getPaymentlistPage(page);
                List<Payment> paymentList1 = paymentService.getAllPayMentlistPage(paymentList);
                tidings.setStatus(Const.SUCCESS);
                tidings.setT(paymentList1).setPage(page);
            }
        } catch (Exception e) {
            e.printStackTrace();
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("数据异常");
        }
        return tidings;
    }
    /*
    * 根据缴费类型查询类型下面所有缴费项目
    * */
    @RequestMapping("merchant/chargeUnderLine/getPaymentByType")
    @ResponseBody
    public Object getPaymentByType() {
        PageData pd = this.getPageData();
        Tidings<List<MProject>> tidings = new Tidings<List<MProject>>();
        String cropId = this.getUser().getCorpId();
        String projectTypeId = pd.getString("projectTypeId");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("cropId", cropId);
        map.put("projectTypeId", projectTypeId);
        try {
            List<MProject> mProjectList = mProjectService.findProjectListByType(map);
            tidings.setStatus(Const.SUCCESS);
            tidings.setT(mProjectList);
        } catch (Exception e) {
            e.printStackTrace();
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("数据异常");
        }
            return tidings;
    }

    @RequestMapping("merchant/chargeUnderLine/chargeUnder")
    @ResponseBody
    public Object chargeUnder(){
        PageData pd = this.getPageData();
        Tidings<String> tidings = new Tidings<String>();
        String cropId = this.getUser().getCorpId();
        String selectPayType=pd.getString("selectPayType");
        String payIds=pd.getString("payId");
        if(StringUtils.isBlank(selectPayType)){
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("请选择支付类型");
            return tidings;
        }
        try {
            if(StringUtils.isBlank(payIds)){
                tidings.setStatus(Const.SUCCESS);
                tidings.setMsg("请选择缴费用户");
                return tidings;
            }else{
                String str[]=payIds.split(",");
                for(String str1:str){
                   Long payid=Long.parseLong(str1);
                    Payment payment=paymentService.getPaymentById(payid,null);
                    payment.setFactAmt(payment.getTotalAmt());
                    payment.setPayType(selectPayType);
                    payment.setPayStatus("P-00");
                    payment.setPayDate(new Date());
                    payment.setUpdateDate(new Date());
                    payment.setPayId(payid);
                    paymentService.underCharge(payment);
                }
                tidings.setStatus(Const.SUCCESS);
                tidings.setMsg("收费成功");
                return tidings;
            }
        } catch (Exception e) {
            e.printStackTrace();
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("收费失败");
        }
        return tidings;
    }

}
